#pragma once

#include <iostream>
#include <string>
#include <cstdarg>
#include <ctime>
#include <unistd.h>

#define DEBUG   0//设置每种错误的数字标识符
#define NORMAL  1
#define WARNING 2
#define ERROR   3
#define FATAL   4

const char * to_levelstr(int level)
{
    switch(level)//通过输入的数字转化为错误原因字符串
    {
        case DEBUG : return "DEBUG";
        case NORMAL: return "NORMAL";
        case WARNING: return "WARNING";
        case ERROR: return "ERROR";
        case FATAL: return "FATAL";
        default : return nullptr;
    }
}

void logMessage(int level, const char *format, ...)//可变参数列表
{
#define NUM 1024    
    char logprefix[NUM];
    snprintf(logprefix, sizeof(logprefix), "[%s][%ld][pid: %d]",
        to_levelstr(level), (long int)time(nullptr), getpid());//打印错误码前缀

    char logcontent[NUM];
    va_list arg;//创建可变参数列表的接收对象
    va_start(arg, format);//获取输入的可变参数
    vsnprintf(logcontent, sizeof(logcontent), format, arg);//将可变参数转化后传给数组

    std::cout << logprefix << logcontent << std::endl;//打印到显示器中
}